package com.sailfish.springbootdemo.service.db7.Impl;


import com.alibaba.fastjson.JSONArray;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.pojo.Result;
import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.dao.db7.TbDataDictionaryDao;
import com.sailfish.springbootdemo.pojo.db7.TbDataDictionary;
import com.sailfish.springbootdemo.service.db7.CommonService;
import com.sailfish.springbootdemo.service.db7.TbDataDictionaryService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class TbDataDictionaryServiceImpl implements TbDataDictionaryService {
    @Autowired
    private TbDataDictionaryDao tbDataDictionaryDao;

    @Autowired
    private CommonService commonService;

    @Override
    public Result getDataDictionaryList(String data) {

        try {
            TbDataDictionary param = new TbDataDictionary();
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                String _param_key = jsonObject.getString("param_key");
                if (_param_key != null && !_param_key.isEmpty()) {
                    param.setParam_key(_param_key);
                    param.setParent_param_key(_param_key);
                }
                String _parent_Id = jsonObject.getString("parent_id");
                if (_parent_Id != null && !_parent_Id.isEmpty()) {
                    try {
                        param.setParent_id(Integer.parseInt(_parent_Id));
                    } catch (Exception e) {
                        return ResultUtil.error(500, "parent_Id 格式不正确", "", null);
                    }
                }
            }

            List<TbDataDictionary> list = tbDataDictionaryDao.getDataDictionaryList(param);
            //
            List<TbDataDictionary> tree = new ArrayList<>();

            //判断key是不是空
            if (param.getParam_key() == null || param.getParam_key().isEmpty()) {
                tree=commonService.buildTree(list);
            }
            else {
                tree=commonService.buildTree(list, param.getParam_key());
            }

            return ResultUtil.success(tree);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }

    @Override
    public Result getDataDictionaryByParamKey(String data) {

        try {
            TbDataDictionary param = new TbDataDictionary();
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                String _param_key = jsonObject.getString("param_key");
                if (_param_key != null && !_param_key.isEmpty()) {
                    param.setParam_key(_param_key);
                    //param.setParent_param_key(_param_key);
                }
                String _parent_select_id = jsonObject.getString("parent_select_id");
                if (_parent_select_id != null && !_parent_select_id.isEmpty()) {
                        param.setParent_select_id(_parent_select_id);
                }
                String _parent_param_key = jsonObject.getString("parent_param_key");
                if (_parent_param_key != null && !_parent_param_key.isEmpty()) {
                    param.setParent_param_key(_parent_param_key);
                    //param.setParent_param_key(_param_key);
                }

            }
            //所有的父节点数据
            List<TbDataDictionary> listDicParent= tbDataDictionaryDao.getDataDictionaryByParamKey(param);

            //所有的节点数据
            List<TbDataDictionary> listAll= tbDataDictionaryDao.getAllDataDictionaryList();

            List<TbDataDictionary> listAllChildren = commonService.getAllChildren(listAll, listDicParent);

            String _tempKey= param.getParam_key()==null?param.getParent_param_key():param.getParam_key();

            if(param.getParam_key()==null)
            {
                return ResultUtil.success(listAllChildren);
            }
            else
            {
                List<TbDataDictionary> listAllChildren2= commonService.buildTree(listAllChildren,_tempKey);

                List<TbDataDictionary> childrenList = new ArrayList<>();

                List<TbDataDictionary> emptyChildrenList = new ArrayList<>();
                // 遍历每个节点的 children 列表
                for (TbDataDictionary node : listAllChildren2) {
                    if (node.getChildren() != null) {
                        childrenList.addAll(node.getChildren());
                    }
                }

                for (TbDataDictionary node : childrenList) {
                    if (node.getChildren() != null) {
                        Collections.sort(node.getChildren(), new Comparator<TbDataDictionary>() {
                            @Override
                            public int compare(TbDataDictionary o1, TbDataDictionary o2) {
                                // 如果 o1 或 o2 的 childrenCount 为 null，则将其视为 0
                                int count1 = o1.getChildrenCount() == null ? 0 : o1.getChildrenCount();
                                int count2 = o2.getChildrenCount() == null ? 0 : o2.getChildrenCount();

                                // 按照 childrenCount 从小到大排序
                                return count1 - count2;
                            }
                        });
                    }else {
                        emptyChildrenList.add(node);
                    }
                }

                childrenList.removeAll(emptyChildrenList);
                childrenList.addAll(emptyChildrenList);

                return ResultUtil.success(childrenList);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }


    @Override
    public Result insertTbDataDictionary(String data) {
        try {
            TbDataDictionary param = new TbDataDictionary();
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                String _parent_id = jsonObject.getString("parent_id");
                String _param_name = jsonObject.getString("param_name");
                String _param_name_en = jsonObject.getString("param_name_en");
                String _param_key = jsonObject.getString("param_key");
                String _parent_param_key = jsonObject.getString("parent_param_key");
                String _param_type = jsonObject.getString("param_type");
                String _level = jsonObject.getString("level");
                String _select_level = jsonObject.getString("select_level");
                String _parent_select_id = jsonObject.getString("parent_select_id");
                String _tip = jsonObject.getString("tip");
                String _remark = jsonObject.getString("remark");
                String _create_user = UserHeaderHolder.getUserId();//jsonObject.getString("create_user");
                //String _create_time = jsonObject.getString("create_time");
                String _status = jsonObject.getString("status");
                String _sort = jsonObject.getString("sort");
                String _is_value = jsonObject.getString("is_value");
                String _is_show = jsonObject.getString("is_show");


                if (_parent_param_key != null && !_parent_param_key.isEmpty()) {
                    param.setParent_param_key(_parent_param_key);
                }
                else
                {
                    return ResultUtil.error(500, "parent_param_key 不能为空", "", null);
                }

                //去查询一下id信息
                List<TbDataDictionary> tempList= tbDataDictionaryDao.getDataDictionaryByParamKey(param);

                if (tempList != null && tempList.size() > 0) {
                    param.setParent_id(tempList.get(0).getId());
                    _parent_id=tempList.get(0).getId().toString();
                }
                else
                {
                    //如果没找到 有可能是添加父节点下面的第一层节点信息
                    //判断是否是第一层节点信息
                    if(_parent_param_key.equals("logInfo")||
                            _parent_param_key.equals("designType")||
                            _parent_param_key.equals("deviceType") ||
                            _parent_param_key.equals("application") ||
                            _parent_param_key.equals("description"))
                    {
                        TbDataDictionary param1 = new TbDataDictionary();
                        param1.setParam_key(_parent_param_key);

                        List<TbDataDictionary> tempList2= tbDataDictionaryDao.getDataDictionaryByParamKey(param);
                        if (tempList2 != null && tempList2.size() > 0) {
                            param.setParent_id(tempList2.get(0).getId());
                            _parent_id=tempList.get(0).getId().toString();
                        }
                        else
                        {
                            return ResultUtil.error(500, "param_key 不存在", "", null);
                        }
                    }
                    else
                    {
                        return ResultUtil.error(500, "param_key 不存在", "", null);
                    }
                }

                if (_parent_id != null && !_parent_id.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_parent_id);
                        param.setParent_id(temp);
                    } catch (NumberFormatException e) {
                        return ResultUtil.error(500, "parent_id 格式错误", "", null);
                    }
                }
                else
                {
                    return ResultUtil.error(500, "parent_id 不能为空", "", null);
                }

                if (_param_name != null && !_param_name.isEmpty()) {
                    param.setParam_name(_param_name);
                }
                else
                {
                    return ResultUtil.error(500, "param_name 不能为空", "", null);
                }

                if (_param_name_en != null && !_param_name_en.isEmpty()) {
                    param.setParam_name_en(_param_name_en);
                }

                if (_param_key != null && !_param_key.isEmpty()) {
                    param.setParam_key(_param_key);
                }
                else
                {
                    return ResultUtil.error(500, "param_key 不能为空", "", null);
                }



                if (_param_type != null && !_param_type.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_param_type);
                        param.setParam_type(temp);
                    } catch (NumberFormatException e) {
                        return ResultUtil.error(500, "param_type 格式错误", "", null);
                    }
                }

                if (_level != null && !_level.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_level);
                        param.setLevel(temp);
                    } catch (NumberFormatException e) {
                        return ResultUtil.error(500, "level 格式错误", "", null);
                    }
                }

                if (_select_level != null && !_select_level.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_select_level);
                        param.setSelect_level(temp);
                    } catch (NumberFormatException e) {
                        return ResultUtil.error(500, "select_level 格式错误", "", null);
                    }
                }

                if (_parent_select_id != null && !_parent_select_id.isEmpty()) {
                    try {
                        //Integer temp=Integer.parseInt(_parent_select_id);
                        param.setParent_select_id(_parent_select_id);
                    } catch (NumberFormatException e) {
                        // Handle the exception if the value is not a number
                    }
                }

                if (_tip != null && !_tip.isEmpty()) {
                    param.setTip(_tip);
                }

                if (_remark != null && !_remark.isEmpty()) {
                    param.setRemark(_remark);
                }

                if (_create_user != null && !_create_user.isEmpty()) {
                    param.setCreate_user(_create_user);
                }

                if (_status != null && !_status.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_status);
                        param.setStatus(temp);
                    } catch (NumberFormatException e) {
                        // Handle the exception if the value is not a number
                    }
                }

                if (_sort != null && !_sort.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_sort);
                        param.setSort(temp);
                    } catch (NumberFormatException e) {
                        // Handle the exception if the value is not a number
                    }
                }

                if (_is_value != null && !_is_value.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_is_value);
                        param.setIs_value(temp);
                    } catch (NumberFormatException e) {
                        // Handle the exception if the value is not a number
                    }
                }

                if (_is_show != null && !_is_show.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_is_show);
                        param.setIs_show(temp);
                    } catch (NumberFormatException e) {
                        // Handle the exception if the value is not a number
                    }
                }

                param.setCreate_time(new Date());

                Integer result= tbDataDictionaryDao.insertDataDictionary(param);

                if (result > 0) {
                    return ResultUtil.success("添加成功");
                }
                else
                {
                    return ResultUtil.error(500, "添加失败", "", null);
                }

            }
            else
            {
                return ResultUtil.error(500, "缺少参数", "", null);
            }

        }
        catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, "添加失败", "", null);
        }
    }

    @Override
    public Result updateDataDictionary(String data) {
        try {
            TbDataDictionary param = new TbDataDictionary();
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                String _id = jsonObject.getString("id");
                String _parent_id = jsonObject.getString("parent_id");
                String _param_name = jsonObject.getString("param_name");
                String _param_name_en = jsonObject.getString("param_name_en");
                String _param_key = jsonObject.getString("param_key");
                String _parent_param_key = jsonObject.getString("parent_param_key");
                String _param_type = jsonObject.getString("param_type");
                String _level = jsonObject.getString("level");
                String _select_level = jsonObject.getString("select_level");
                String _parent_select_id = jsonObject.getString("parent_select_id");
                String _tip = jsonObject.getString("tip");
                String _remark = jsonObject.getString("remark");
                String _update_user = UserHeaderHolder.getUserId();//jsonObject.getString("update_user");
                //String _create_time = jsonObject.getString("create_time");
                String _status = jsonObject.getString("status");
                String _sort = jsonObject.getString("sort");
                String _is_value = jsonObject.getString("is_value");
                String _is_show = jsonObject.getString("is_show");


                if (_id != null && !_id.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_id);
                        param.setId(temp);

                    }
                    catch (NumberFormatException e) {
                        return ResultUtil.error(500, "id 格式错误", "", null);
                    }
                }
                else{
                    return ResultUtil.error(500, "id 不能为空", "", null);
                }

                if (_parent_id != null && !_parent_id.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_parent_id);
                        param.setParent_id(temp);
                    } catch (NumberFormatException e) {
                        return ResultUtil.error(500, "parent_id 格式错误", "", null);
                    }
                }
                else
                {
                    return ResultUtil.error(500, "parent_id 不能为空", "", null);
                }

                if (_param_name != null && !_param_name.isEmpty()) {
                    param.setParam_name(_param_name);
                }
                else
                {
                    return ResultUtil.error(500, "param_name 不能为空", "", null);
                }

                if (_param_name_en != null && !_param_name_en.isEmpty()) {
                    param.setParam_name_en(_param_name_en);
                }

                if (_param_key != null && !_param_key.isEmpty()) {
                    param.setParam_key(_param_key);
                }
                else
                {
                    return ResultUtil.error(500, "param_key 不能为空", "", null);
                }

                if (_parent_param_key != null && !_parent_param_key.isEmpty()) {
                    param.setParent_param_key(_parent_param_key);
                }
                else
                {
                    return ResultUtil.error(500, "parent_param_key 不能为空", "", null);
                }

                if (_param_type != null && !_param_type.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_param_type);
                        param.setParam_type(temp);
                    } catch (NumberFormatException e) {
                        return ResultUtil.error(500, "param_type 格式错误", "", null);
                    }
                }

                if (_level != null && !_level.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_level);
                        param.setLevel(temp);
                    } catch (NumberFormatException e) {
                        return ResultUtil.error(500, "level 格式错误", "", null);
                    }
                }

                if (_select_level != null && !_select_level.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_select_level);
                        param.setSelect_level(temp);
                    } catch (NumberFormatException e) {
                        return ResultUtil.error(500, "select_level 格式错误", "", null);
                    }
                }

                if (_parent_select_id != null && !_parent_select_id.isEmpty()) {
                    try {
                        //Integer temp=Integer.parseInt(_parent_select_id);
                        param.setParent_select_id(_parent_select_id);
                    } catch (NumberFormatException e) {
                        // Handle the exception if the value is not a number
                    }
                }

                if (_tip != null && !_tip.isEmpty()) {
                    param.setTip(_tip);
                }

                if (_remark != null && !_remark.isEmpty()) {
                    param.setRemark(_remark);
                }

                if (_update_user != null && !_update_user.isEmpty()) {
                    param.setUpdate_user(_update_user);
                }

                if (_status != null && !_status.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_status);
                        param.setStatus(temp);
                    } catch (NumberFormatException e) {
                        // Handle the exception if the value is not a number
                    }
                }

                if (_sort != null && !_sort.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_sort);
                        param.setSort(temp);
                    } catch (NumberFormatException e) {
                        // Handle the exception if the value is not a number
                    }
                }

                if (_is_value != null && !_is_value.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_is_value);
                        param.setIs_value(temp);
                    } catch (NumberFormatException e) {
                        // Handle the exception if the value is not a number
                    }
                }
                if (_is_show != null && !_is_show.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_is_show);
                        param.setIs_show(temp);
                    } catch (NumberFormatException e) {
                        // Handle the exception if the value is not a number
                    }
                }

                param.setUpdate_time(new Date());

                Integer result= tbDataDictionaryDao.updateDataDictionary(param);
                if (result > 0) {
                    return ResultUtil.success("更新成功");
                }
                else
                {
                    return ResultUtil.error(500, "更新失败", "", null);
                }

            }
            else
            {
                return ResultUtil.error(500, "缺少参数", "", null);
            }

        }
        catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, "更新失败", "", null);
        }
    }

    @Override
    public Result deleteDataDictionary(String data) {
        try {
            TbDataDictionary param = new TbDataDictionary();
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                String _id = jsonObject.getString("id");

                if (_id != null && !_id.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_id);
                        param.setId(temp);
                    }
                    catch (NumberFormatException e) {
                        return ResultUtil.error(500, "id 格式错误", "", null);
                    }
                }
                else{
                    return ResultUtil.error(500, "id 不能为空", "", null);
                }

                Integer result= tbDataDictionaryDao.deleteDataDictionary(param.id);

                if (result > 0) {
                    return ResultUtil.success("删除成功");
                }
                else
                {
                    return ResultUtil.error(500, "删除失败", "", null);
                }
            }
            else
            {
                return ResultUtil.error(500, "缺少参数", "", null);
            }

        }
        catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, "更新失败", "", null);
        }
    }

    @Transactional(transactionManager = "MysqlTransactionManager7")
    @Override
    public Result insertTbDataDictionaryBatch(String data) {
        try {
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                // 解析Json字符串
                JSONObject jsonObject1 = JSONObject.parseObject(data);
                JSONArray jsonArray = jsonObject1.getJSONArray("list");

                List<TbDataDictionary> insertDataDictionaryList = new ArrayList<>();
                List<TbDataDictionary> updateDataDictionaryList = new ArrayList<>();
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    TbDataDictionary param = new TbDataDictionary();

                    String _id = jsonObject.getString("id");
                    String _parent_id =""; //jsonObject.getString("parent_id");
                    String _param_name = jsonObject.getString("param_name");
                    String _param_name_en = jsonObject.getString("param_name_en");
                    String _param_key = jsonObject.getString("param_key");
                    String _parent_param_key = jsonObject.getString("parent_param_key");
                    String _param_type = jsonObject.getString("param_type");
                    String _level = jsonObject.getString("level");
                    String _select_level = jsonObject.getString("select_level");
                    String _parent_select_id = jsonObject.getString("parent_select_id");
                    String _tip = jsonObject.getString("tip");
                    String _remark = jsonObject.getString("remark");
                    String _create_user = UserHeaderHolder.getUserId();//jsonObject.getString("create_user");
                    //String _create_time = jsonObject.getString("create_time");
                    String _update_user = UserHeaderHolder.getUserId();
                    String _status = jsonObject.getString("status");
                    String _sort = jsonObject.getString("sort");
                    String _is_value = jsonObject.getString("is_value");
                    String _is_show= jsonObject.getString("is_show");

                    if (_id != null && !_id.isEmpty()) {
                        try {
                            Integer temp=Integer.parseInt(_id);
                            param.setId(temp);
                        } catch (NumberFormatException e) {
                            return ResultUtil.error(500, "id 格式错误", "", null);
                        }
                    }
                    else
                    {
                        return ResultUtil.error(500, "id 不能为空", "", null);
                    }

                    if (_parent_param_key != null && !_parent_param_key.isEmpty()) {
                        param.setParent_param_key(_parent_param_key);
                    }
                    else
                    {
                        return ResultUtil.error(500, "parent_param_key 不能为空", "", null);
                    }

                    //去查询一下id信息
                    List<TbDataDictionary> tempList= tbDataDictionaryDao.getDataDictionaryParentId(_parent_param_key);

                    if (tempList != null && tempList.size() > 0) {
                        param.setParent_id(tempList.get(0).getId());
                        _parent_id=tempList.get(0).getId().toString();
                    }
                    else
                    {
                        return ResultUtil.error(500, "parent_param_key 不存在", "", null);
                    }

                    if (_parent_id != null && !_parent_id.isEmpty()) {
                        try {
                            Integer temp=Integer.parseInt(_parent_id);
                            param.setParent_id(temp);
                        } catch (NumberFormatException e) {
                            return ResultUtil.error(500, "parent_id 格式错误", "", null);
                        }
                    }
                    else
                    {
                        return ResultUtil.error(500, "parent_id 不能为空", "", null);
                    }

                    if (_param_name != null && !_param_name.isEmpty()) {
                        param.setParam_name(_param_name);
                    }
                    else
                    {
                        return ResultUtil.error(500, "param_name 不能为空", "", null);
                    }

                    if (_param_name_en != null && !_param_name_en.isEmpty()) {
                        param.setParam_name_en(_param_name_en);
                    }

                    if (_param_key != null && !_param_key.isEmpty()) {
                        param.setParam_key(_param_key);
                    }
                    else
                    {
                        return ResultUtil.error(500, "param_key 不能为空", "", null);
                    }

                    if (_param_type != null && !_param_type.isEmpty()) {
                        try {
                            Integer temp=Integer.parseInt(_param_type);
                            param.setParam_type(temp);
                        } catch (NumberFormatException e) {
                            return ResultUtil.error(500, "param_type 格式错误", "", null);
                        }
                    }

                    if (_level != null && !_level.isEmpty()) {
                        try {
                            Integer temp=Integer.parseInt(_level);
                            param.setLevel(temp);
                        } catch (NumberFormatException e) {
                            return ResultUtil.error(500, "level 格式错误", "", null);
                        }
                    }

                    if (_select_level != null && !_select_level.isEmpty()) {
                        try {
                            Integer temp=Integer.parseInt(_select_level);
                            param.setSelect_level(temp);
                        } catch (NumberFormatException e) {
                            return ResultUtil.error(500, "select_level 格式错误", "", null);
                        }
                    }

                    if (_parent_select_id != null && !_parent_select_id.isEmpty()) {
                        try {
                            //Integer temp=Integer.parseInt(_parent_select_id);
                            param.setParent_select_id(_parent_select_id);
                        } catch (NumberFormatException e) {
                            // Handle the exception if the value is not a number
                        }
                    }

                    if (_tip != null && !_tip.isEmpty()) {
                        param.setTip(_tip);
                    }

                    if (_remark != null && !_remark.isEmpty()) {
                        param.setRemark(_remark);
                    }

                    if (_create_user != null && !_create_user.isEmpty()) {
                        param.setCreate_user(_create_user);
                    }

                    if (_update_user != null && !_update_user.isEmpty()) {
                        param.setUpdate_user(_update_user);
                    }

                    if (_status != null && !_status.isEmpty()) {
                        try {
                            Integer temp=Integer.parseInt(_status);
                            param.setStatus(temp);
                        } catch (NumberFormatException e) {
                            // Handle the exception if the value is not a number
                        }
                    }

                    if (_sort != null && !_sort.isEmpty()) {
                        try {
                            Integer temp=Integer.parseInt(_sort);
                            param.setSort(temp);
                        } catch (NumberFormatException e) {
                            // Handle the exception if the value is not a number
                        }
                    }

                    if (_is_value != null && !_is_value.isEmpty()) {
                        try {
                            Integer temp=Integer.parseInt(_is_value);
                            param.setIs_value(temp);
                        } catch (NumberFormatException e) {
                            // Handle the exception if the value is not a number
                        }
                    }
                    if (_is_show != null && !_is_show.isEmpty()) {
                        try {
                            Integer temp=Integer.parseInt(_is_show);
                            param.setIs_show(temp);
                        } catch (NumberFormatException e) {
                            // Handle the exception if the value is not a number
                        }
                    }

                    param.setCreate_time(new Date());
                    param.setUpdate_time(new Date());



                    if(param.getId()>0)
                    {
                        updateDataDictionaryList.add(param);

                    }
                    else
                    {
                        insertDataDictionaryList.add(param);
                    }
                }
                Integer result1=0;
                if (updateDataDictionaryList.size() > 0) {

                    for (int i = 0; i < updateDataDictionaryList.size(); i++)
                    {
                        result1=result1+tbDataDictionaryDao.updateDataDictionaryBatch(updateDataDictionaryList.get(i));
                    }
                }
                Integer result=0;

                if (insertDataDictionaryList.size() > 0) {
                    result=tbDataDictionaryDao.insertDataDictionaryBatch(insertDataDictionaryList);
                }

                if (result > 0||result1>0) {
                    return ResultUtil.success("操作成功");
                }
                else
                {
                    return ResultUtil.error(500, "操作失败", "", null);
                }

            }
            else
            {
                return ResultUtil.error(500, "缺少参数", "", null);
            }

        }
        catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }


    @Override
    public Result getDataDictionarySelectLevel(String data) {

        try {
            TbDataDictionary param = new TbDataDictionary();
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                String _parent_param_key= jsonObject.getString("parent_param_key");
                if (_parent_param_key != null && !_parent_param_key.isEmpty()) {
                    param.setParent_param_key(_parent_param_key);
                }
                String _param_type = jsonObject.getString("param_type");
                if (_param_type != null && !_param_type.isEmpty()) {
                    try {
                        param.setParam_type(Integer.parseInt(_param_type));
                    } catch (Exception e) {
                        return ResultUtil.error(500, "param_type 格式不正确", "", null);
                    }
                }

                String _select_level = jsonObject.getString("select_level");
                if (_select_level != null && !_select_level.isEmpty()) {
                    try {
                        param.setSelect_level(Integer.parseInt(_select_level));
                    } catch (Exception e) {
                        return ResultUtil.error(500, "select_level 格式不正确", "", null);
                    }
                }

            }

            List<TbDataDictionary> list = tbDataDictionaryDao.getDataDictionaryByParamKey(param);


            return ResultUtil.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }
}
